<style>
    #user-update {
        padding: 20px 25px 25px 0;
    }
</style>
<div class="layui-fluid" id="user-update">
    <form class="layui-form" action="" lay-filter="user-update-form">
        <div class="layui-form-item qiu-hide">
            <label class="layui-form-label qiu-form-item-require">用户id：</label>
            <div class="layui-input-block">
                <input type="text" name="id" data-th-value="${user.id}">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label qiu-form-item-require">用户名：</label>
            <div class="layui-input-block">
                <input type="text" name="username" minlength="4" maxlength="10" data-th-id="${user.id}"
                       lay-verify="range|username" autocomplete="off" class="layui-input" readonly>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">邮箱：</label>
            <div class="layui-input-block">
                <input type="text" name="email" lay-verify="email" maxlength="50" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label qiu-form-item-require">角色：</label>
            <div class="layui-input-block" id="user-update-role"></div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label qiu-form-item-require">状态：</label>
            <div class="layui-input-block">
                <input type="radio" name="status" value="1" title="有效">
                <input type="radio" name="status" value="0" title="禁用">
            </div>
        </div>

        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注：</label>
            <div class="layui-input-block">
                <textarea name="description" maxlength="100" class="layui-textarea">
                </textarea>
            </div>
        </div>
        <div class="layui-form-item qiu-hide">
            <button class="layui-btn" lay-submit="" lay-filter="user-update-form-submit" id="submit"></button>
        </div>
    </form>
</div>

<script data-th-inline="javascript">
    layui.use(['qiu', 'form', 'xmSelect', 'validate'], function () {
        let $ = layui.$,
            qiu = layui.qiu,
            layer = layui.layer,
            form = layui.form,
            xmSelect = layui.xmSelect,
            user = [[${user}]],
            validate = layui.validate,
            roleXmSelect;

        form.verify(validate);
        form.render();

        initUserValue();

        roleXmSelect = xmSelect.render({
            el: '#user-update-role',
            toolbar: {show: true},
            name: 'roleId',
            theme: {
                color: '#32c787',
            },
            prop: {
                name: 'roleName',
                value: 'roleId'
            },
            data: []
        });

        qiu.get(ctx + 'role', null, function (data) {
            roleXmSelect.update({
                data: data.data,
                autoRow: true,
            })
            roleXmSelect.setValue(user.roleId.split(','));
        });

        function initUserValue() {
            form.val("user-update-form", {
                "username": user.username,
                "email": user.email,
                "status": user.status,
                "description": user.description
            });
        }

        form.on('submit(user-update-form-submit)', function (data) {
            if (!data.field.roleId) {
                qiu.alert.warn('请选择用户角色');
                return false;
            }
            if (qiu.nativeEqual(data.field, user)) {
                qiu.alert.warn('数据未作任何修改！');
                return false;
            }
            qiu.post(ctx + 'user/update', data.field, function () {
                layer.closeAll();
                qiu.alert.success(user.username + ' 用户数据修改成功');
                $('#qiu-user').find('#query').click();
            });
            return false;
        });
    });
</script>